import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";
import { UtilsProvider } from '../../providers/utils/utils';
import {ConfigProvider} from "../../providers/config/config";

@IonicPage({
    segment: 'member/enlist/:amoy_id',
    defaultHistory: ['MemberPage', 'PersonRewardPage']
})
@Component({
    selector: 'page-enlist',
    templateUrl: 'enlist.html',
})
export class EnlistPage {
    infoData = {};
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService:HttpServicesProvider,
        public storage:StorageProvider,
        public validate:ValidateProvider,
        public util: UtilsProvider,
        public config: ConfigProvider
    ) {
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }
    ionViewWillEnter(){
        //文章ID
        this.getData(null);
    }

    page = 1;
    lists = [];
    getData(infiniteScroll){
        this.httpService.requestPostData('/task_info/apply_list', {
            token: this.config.getToken(),
            amoy_id: this.navParams.get('amoy_id'),
            page: this.page
        }, (response) => {
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['lists'].length < data['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    choose(user_id) {
        this.util.presentConfirm('确定要选择当前用户开始任务吗？', ()=>{
            this.httpService.requestPostData('/task_info/change_status', {
                token: this.config.getToken(),
                id: this.navParams.get('amoy_id'),
                user_id: user_id,
                status: 1
            }, data => {
                if(data['error'] == 200) {
                    this.util.presentAlert('操作成功');
                    this.getData(null);
                    this.navCtrl.push('EnlistPage')
                } else {
                    this.util.presentAlert(data.message);
                }
            });
        });
    }
}
